package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.CompanyDao;
import com.cn.wanxi.dao.NavDao;
import com.cn.wanxi.model.NavModel;
import com.cn.wanxi.service.NavService;
import com.cn.wanxi.util.IO;
import com.cn.wanxi.util.Path;
import com.cn.wanxi.util.Tool;
import com.sun.deploy.util.ArrayUtil;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class NavServiceImpl implements NavService {
    @Override
    public List<NavModel> getNavModelList() {
////        1.创建文件
//        File file = createNote();
//        2.写入数据
//        String path = "D:\\wanxi\\step-3\\canyin03-0301\\web\\note";
//        String fileName = "nav.txt";

        setWriteList(Path.NAV_NAME.getName());
//        3.读取数据
        String result = IO.readNote();
//        4.封装数据
        return setNavModelList(result);
    }

    private void setWriteList(String fileName) {
        List<String> list = new ArrayList<>();
        NavDao navDao = new NavDao();
        list.add(Tool.arrayToString(navDao.hrefs));
        list.add(Tool.arrayToString(navDao.titles));
        IO.writeNote(fileName, list);
    }

    private List<NavModel> setNavModelList(String result) {
        System.out.println(result);
        String[] results = result.split(Path.SPLIT.getName());
        String href = results[0];
        String title = results[1];

        List<NavModel> list = new ArrayList<>();
        String[] hrefs = href.split(",");
        String[] titles = title.split(",");
        int leng = hrefs.length;
        for (int i = 0; i < leng; i++) {
            NavModel model = new NavModel();
            model.setHref(hrefs[i]);
            model.setTitle(titles[i]);
            list.add(model);
        }
        return list;
    }
//
//    private void writeNote(File file) {
//        try {
////         所有的引用类型的父类都是   Object类
//            OutputStream outputStream = new FileOutputStream(file);
//
//            NavDao navDao = new NavDao();
//            String[] hrefs = navDao.hrefs;
//            StringBuilder builder = new StringBuilder();
////            方法名相同，参数不同，类型不同，个数不同  重载
////            重写 与  重载的区别？
////            /home,/company,/company,/company,/company,/company
////            for (String a : hrefs) {
////                builder.append(a).append(",");
////            }
////            System.out.println(builder.substring(0,builder.length()-1));
//
////            System.out.println(Arrays.toString(hrefs));
//            outputStream.write(Tool.arrayToString(hrefs).getBytes());
//            outputStream.write(IO.SPLIT.getBytes());
//            outputStream.write(Tool.arrayToString(navDao.titles).getBytes());
//
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }


}
